package com.example.demo.crud.service.impl;

import com.example.demo.crud.entity.SysPersonEntity;
import com.example.demo.crud.mapper.SysPersonMapper;
import com.example.demo.crud.service.SysPersonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 *  服务实现类
 *
 * @author CustomGenerator
 * @since 2025-08-24 14:49:15
 */
@Slf4j
@Service
public class SysPersonServiceImpl extends ServiceImpl<SysPersonMapper, SysPersonEntity> implements SysPersonService {

    @Override
    public SysPersonEntity getByIdWithDetails(Long id) {
        if (id == null) {
            log.warn("查询详情时，ID不能为空");
            return null;
        }
        try {
            return baseMapper.selectByIdWithDetails(id);
        } catch (Exception e) {
            log.error("查询详情失败，ID: {}", id, e);
            throw new RuntimeException("查询详情失败", e);
        }
    }

    @Override
    public IPage<SysPersonEntity> pageList(Page<SysPersonEntity> page, SysPersonEntity entity) {
        try {
            return baseMapper.selectPageList(page, entity);
        } catch (Exception e) {
            log.error("分页查询列表失败", e);
            throw new RuntimeException("分页查询列表失败", e);
        }
    }

    @Override
    public List<SysPersonEntity> listByCondition(SysPersonEntity entity) {
        try {
            return baseMapper.selectList(entity);
        } catch (Exception e) {
            log.error("查询列表失败", e);
            throw new RuntimeException("查询列表失败", e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean create(SysPersonEntity entity) {
        if (entity == null) {
            log.warn("新增时，实体不能为空");
            return false;
        }
        try {
            // 可以在这里添加业务逻辑验证
            validateEntity(entity);
            
            int result = baseMapper.insert(entity);
            if (result > 0) {
                log.info("新增成功，ID: {}", entity.);
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("新增失败", e);
            throw new RuntimeException("新增失败", e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysPersonEntity entity) {
        if (entity == null || entity. == null) {
            log.warn("更新时，实体或ID不能为空");
            return false;
        }
        try {
            // 检查记录是否存在
            SysPersonEntity existingEntity = baseMapper.selectById(entity.);
            if (existingEntity == null) {
                log.warn("更新时，记录不存在，ID: {}", entity.);
                return false;
            }
            
            // 可以在这里添加业务逻辑验证
            validateEntity(entity);
            
            int result = baseMapper.updateById(entity);
            if (result > 0) {
                log.info("更新成功，ID: {}", entity.);
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("更新失败", e);
            throw new RuntimeException("更新失败", e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        if (id == null) {
            log.warn("删除时，ID不能为空");
            return false;
        }
        try {
            // 检查记录是否存在
            SysPersonEntity existingEntity = baseMapper.selectById(id);
            if (existingEntity == null) {
                log.warn("删除时，记录不存在，ID: {}", id);
                return false;
            }
            
            int result = baseMapper.deleteById(id);
            if (result > 0) {
                log.info("删除成功，ID: {}", id);
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("删除失败，ID: {}", id, e);
            throw new RuntimeException("删除失败", e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatchByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            log.warn("批量删除时，ID列表不能为空");
            return false;
        }
        try {
            int result = baseMapper.deleteBatchIds(ids);
            if (result > 0) {
                log.info("批量删除成功，数量: {}", result);
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("批量删除失败", e);
            throw new RuntimeException("批量删除失败", e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createBatch(List<SysPersonEntity> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            log.warn("批量新增时，实体列表不能为空");
            return false;
        }
        try {
            // 验证每个实体
            for (SysPersonEntity entity : entityList) {
                validateEntity(entity);
            }
            
            int result = baseMapper.insertBatch(entityList);
            if (result > 0) {
                log.info("批量新增成功，数量: {}", result);
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("批量新增失败", e);
            throw new RuntimeException("批量新增失败", e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBatch(List<SysPersonEntity> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            log.warn("批量更新时，实体列表不能为空");
            return false;
        }
        try {
            // 验证每个实体
            for (SysPersonEntity entity : entityList) {
                if (entity. == null) {
                    throw new IllegalArgumentException("批量更新时，每个实体的ID不能为空");
                }
                validateEntity(entity);
            }
            
            int result = baseMapper.updateBatch(entityList);
            if (result > 0) {
                log.info("批量更新成功，数量: {}", result);
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("批量更新失败", e);
            throw new RuntimeException("批量更新失败", e);
        }
    }

    @Override
    public long countByCondition(SysPersonEntity entity) {
        try {
            return baseMapper.countByCondition(entity);
        } catch (Exception e) {
            log.error("统计数量失败", e);
            throw new RuntimeException("统计数量失败", e);
        }
    }

    @Override
    public boolean exists(SysPersonEntity entity) {
        try {
            long count = baseMapper.countByCondition(entity);
            return count > 0;
        } catch (Exception e) {
            log.error("检查是否存在失败", e);
            throw new RuntimeException("检查是否存在失败", e);
        }
    }

    /**
     * 验证实体数据
     *
     * @param entity 实体对象
     */
    private void validateEntity(SysPersonEntity entity) {
        if (entity == null) {
            throw new IllegalArgumentException("实体不能为空");
        }
        
        // 可以在这里添加具体的业务验证逻辑
        // 例如：必填字段验证、数据格式验证、业务规则验证等
        
    }
}
